package cn.wolfcode.service.impl;

import cn.wolfcode.domain.UserInfo;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.redis.impl.UserInfoRedisServiceImpl;
import cn.wolfcode.repsitory.UserInfoRepsitory;
import cn.wolfcode.service.IUserInfoService;
import cn.wolfcode.util.AssertUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: UserInfo业务逻辑实现类
 * @Author: 伍家钰
 * @Date: 2020/8/7 16:20
 * @Version: 1.0
 */
@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Resource
    private UserInfoRepsitory userInfoRepsitory;
    @Resource
    private UserInfoRedisServiceImpl userInfoRedisService;

    @Override
    public void save(UserInfo userInfo) {
        userInfoRepsitory.save(userInfo);
    }

    @Override
    public void delete(String id) {
        userInfoRepsitory.deleteById(id);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoRepsitory.save(userInfo);
    }

    @Override
    public UserInfo get(String id) {
       return userInfoRepsitory.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> list() {
        return userInfoRepsitory.findAll();
    }

    @Override
    public String checkPhone(String phone) {
        return userInfoRepsitory.findByphone(phone);
    }

    @Override
    public String checkName(String name) {
        return userInfoRepsitory.findByNickname(name);
    }

    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        // 1.校验参数是否为空
        AssertUtil.isEquals(phone, "手机号不能为空");
        AssertUtil.isEquals(nickname, "昵称不能为空");
        AssertUtil.isEquals(password, "密码不能为空");
        AssertUtil.isEquals(rpassword, "确认密码不能为空");
        AssertUtil.isEquals(verifyCode, "验证码不能为空");

        // 2.校验密码和验证密码是否一致
        AssertUtil.judgePasswordIsConsisten(password, rpassword, "密码和验证密码不一致");

        // 3.校验手机号是否唯一
        if (this.checkPhone(phone) != null) {
            throw new LogicException("手机号已经被注册过了");
        }

        // 4.校验短信验证码是否正确
        String code = userInfoRedisService.getCodeByPhone(phone);
        AssertUtil.isEquals(code, "请先获取验证码");
        AssertUtil.isEquals(verifyCode, "验证码不能为空");
        if (!code.equalsIgnoreCase(verifyCode)) {
            throw new LogicException("验证码错误");
        }

        // 5.注册
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setEmail("");
        userInfo.setPassword(password);
        userInfo.setGender(0);
        userInfo.setCity("");
        userInfo.setHeadImgUrl("/images/default.png");
        userInfo.setInfo("");
        // 核心状态自己控制
        userInfo.setState(UserInfo.STATE_NORMAL);
        userInfoRepsitory.save(userInfo);
    }

    @Override
    public UserInfo findByPhoneAndPassword(String username, String password) {
        UserInfo userInfo = userInfoRepsitory.findByPhoneAndPassword(username, password);
        // 如果用户为空则登入失败
        if (userInfo == null) {
            throw new LogicException("账号或密码错误!");
        }
        return userInfo;
    }
}
